﻿Imports System.Drawing

Namespace UI

    Public Class ImgCode
        Inherits System.Web.UI.Page

        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            Response.CacheControl = "no-cache"
            CreateCheckCodeImage(GenerateCheckNumCode)
        End Sub

        Private Function GenerateCheckNumCode() As String
            Dim random As New ThreadSafeRandom
            Dim checkCode As String = String.Empty
            checkCode = Left(CStr(random.NextNum()), 6)

            Dim cookie As New HttpCookie("CheckCode", checkCode)
            Response.Cookies.Add(cookie)
            cookie = Nothing
            random = Nothing
            Return checkCode
        End Function


        'Private Function GenerateCheckCode() As String
        '    Dim number As Integer
        '    Dim code As String
        '    Dim checkCode As String = String.Empty
        '    Dim random As System.Random = New Random
        '    Dim i As Integer = 0
        '    For i = 1 To 3
        '        number = random.Next
        '        If number Mod 2 = 0 Then
        '            code = CType((Chr(CInt(Int(random.NextDouble * 9 + 48))).ToString + (number Mod 10).ToString.Substring(0, 1)), String)
        '        Else
        '            code = CType((Chr(CInt(Int(random.NextDouble * 25 + 97))).ToString + (number Mod 26).ToString.Substring(0, 1)), String)
        '        End If
        '        checkCode += code.ToString
        '    Next
        '    Dim cookie As New HttpCookie("CheckCode", checkCode)
        '    Response.Cookies.Add(cookie)
        '    cookie = Nothing
        '    random = Nothing
        '    Return checkCode
        'End Function

        Private Sub CreateCheckCodeImage(ByVal checkCode As String)
            If checkCode Is Nothing OrElse checkCode.Trim = String.Empty Then
                Return
            End If
            Dim image As System.Drawing.Bitmap = New System.Drawing.Bitmap(CType(Math.Ceiling((checkCode.Length * 15)), Integer), 24)
            Dim g As System.Drawing.Graphics = System.Drawing.Graphics.FromImage(image)
            Try
                Dim random As Random = New Random
                g.Clear(System.Drawing.Color.White)
                Dim i As Integer = 0
                While i < 25
                    Dim x1 As Integer = random.Next(image.Width)
                    Dim x2 As Integer = random.Next(image.Width)
                    Dim y1 As Integer = random.Next(image.Height)
                    Dim y2 As Integer = random.Next(image.Height)
                    g.DrawLine(New System.Drawing.Pen(System.Drawing.Color.Silver), x1, y1, x2, y2)
                    System.Math.Min(System.Threading.Interlocked.Increment(i), i - 1)
                End While
                Dim font As System.Drawing.Font = New System.Drawing.Font("Verdana", 14)
                Dim brush As System.Drawing.Drawing2D.LinearGradientBrush = New System.Drawing.Drawing2D.LinearGradientBrush(New System.Drawing.Rectangle(0, 0, image.Width, image.Height), System.Drawing.Color.Blue, System.Drawing.Color.DarkRed, 1.2F, True)
                g.DrawString(checkCode, font, brush, 2, 0)
                i = 0
                While i < 100
                    Dim x As Integer = random.Next(image.Width)
                    Dim y As Integer = random.Next(image.Height)
                    image.SetPixel(x, y, System.Drawing.Color.FromArgb(random.Next))
                    System.Math.Min(System.Threading.Interlocked.Increment(i), i - 1)
                End While
                g.DrawRectangle(New System.Drawing.Pen(System.Drawing.Color.Silver), 0, 0, image.Width - 1, image.Height - 1)
                Dim ms As System.IO.MemoryStream = New System.IO.MemoryStream
                image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif)
                Response.ClearContent()
                Response.ContentType = "image/gif"
                Response.BinaryWrite(ms.ToArray)
                ms.Close()
                ms = Nothing
                font = Nothing
                brush = Nothing
                random = Nothing
            Finally
                image = Nothing
                g = Nothing
            End Try
        End Sub



    End Class

    Public Class ThreadSafeRandom
        Private random As New Random()

        Public Function NextNum() As Integer
            SyncLock random
                Return random.Next()
            End SyncLock
        End Function



    End Class

End Namespace
